package com.drinks.yiniang.ui.fragment.user.password;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.io.api.UserApiIO;
import com.drinks.yiniang.utils.SharedPreUtils;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.fragment.BaseFragment;
import com.hansen.library.utils.LogUtils;
import com.hansen.library.utils.StringUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class FirstPayPasswordSetFragment extends BaseFragment {

    //输入密码
    private EditText et_first_set_pay_pwd;
    //确认密码
    private EditText et_first_set_pay_pwd_repeat;
    //错误提示
    private TextView tv_first_set_pay_pwd_error_tips;
    //确认
    private Button btn_first_set_pay_pwd_sure;

    public FirstPayPasswordSetFragment() {
        // Required empty public constructor
    }

    public static FirstPayPasswordSetFragment newInstance() {
        return new FirstPayPasswordSetFragment();
    }

    @Override
    protected int getResID() {
        return R.layout.fragment_first_pay_password_set;
    }

    @Override
    protected void initView(View mView) {
        et_first_set_pay_pwd = mView.findViewById(R.id.et_first_set_pay_pwd);
        et_first_set_pay_pwd_repeat = mView.findViewById(R.id.et_first_set_pay_pwd_repeat);

        tv_first_set_pay_pwd_error_tips = mView.findViewById(R.id.tv_first_set_pay_pwd_error_tips);

        btn_first_set_pay_pwd_sure = mView.findViewById(R.id.btn_first_set_pay_pwd_sure);
    }

    @Override
    protected void initData() {
        mContext = getActivity();
    }

    @Override
    protected void listener() {
        btn_first_set_pay_pwd_sure.setOnClickListener(this);
    }

    @Override
    protected void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.btn_first_set_pay_pwd_sure://确认
                setPayPassword();
                break;
            default:
                break;
        }
    }

    private void setPayPassword() {
        String pwd = StringUtils.getEditTextString(et_first_set_pay_pwd.getText());

        if (StringUtils.isEmpty(pwd)) {
            //ToastUtils.showShort(R.string.text_hint_input_password);
            tv_first_set_pay_pwd_error_tips.setText("*" + getString(R.string.text_hint_input_password));
            return;
        }

        if (pwd.length() != 6) {
            //ToastUtils.showShort(R.string.text_hint_input_six_pay_pwd);
            tv_first_set_pay_pwd_error_tips.setText("*" + getString(R.string.text_hint_input_six_pay_pwd));
            return;
        }

        String repeatPwd = StringUtils.getEditTextString(et_first_set_pay_pwd_repeat.getText());

        if (StringUtils.isEmpty(repeatPwd)) {
            //ToastUtils.showShort(R.string.text_hint_input_repeat_pwd);
            tv_first_set_pay_pwd_error_tips.setText("*" + getString(R.string.text_hint_input_repeat_pwd));
            return;
        }

        if (!pwd.equals(repeatPwd)) {
            //ToastUtils.showShort(R.string.text_hint_two_input_pwd_not_consistent);
            tv_first_set_pay_pwd_error_tips.setText("*" + getString(R.string.text_hint_two_input_pwd_not_consistent));
            return;
        }

        tv_first_set_pay_pwd_error_tips.setText("");

        showLoadingDialog();

        UserApiIO.getInstance().setPayPassword("1", pwd, new APIRequestCallback<StringJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(StringJson res) {
                ToastUtils.showShort(R.string.text_pwd_set_success);

                SharedPreUtils.getInstance().saveIsOpenPayPasswordID(Constants.STATUS_GLOBAL_YES);

                if (getActivity() != null) {
                    getActivity().finish();
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }
}
